package com.atguigu.gmall.cart.controller;


import com.atguigu.gmall.cart.service.CartInfoService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/*购物车管理*/
@RestController
@RequestMapping("/api/cart")
public class CartApiController {


    @Autowired
    private CartInfoService cartInfoService;

    //加入购物车
    @PostMapping("/addCart/{skuId}/{skuNum}")
    public CartInfo addCart(@PathVariable Long skuId, @PathVariable Integer skuNum){

        CartInfo cartInfo = cartInfoService.addCart(skuId,skuNum);
        return cartInfo;
    }

    //查询新添加的购物车商品
    @PostMapping("/toCart/{skuId}")
    public CartInfo toCart(@PathVariable Long skuId){
        return cartInfoService.toCart(skuId);
    }



    //去购物车结算
    @GetMapping("/cartList")
    public Result cartList(HttpServletRequest request){
        //真实用户ID
        String userId = AuthContextHolder.getUserId(request);
        //临时用户Id
        String userTempId = AuthContextHolder.getUserTempId(request);
        return Result.ok(cartInfoService.cartList(userId,userTempId));

    }


    //复选框
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable Long skuId,@PathVariable Integer isChecked) {
        cartInfoService.checkCart(skuId, isChecked);
        return Result.ok();
    }

    //商品清单
    @GetMapping("/getCartListChecked")
    public List<CartInfo> getCartListChecked(HttpServletRequest request){
        String userId = AuthContextHolder.getUserId(request);
        return cartInfoService.getCartListChecked(userId);
    }
}
